<template>
  <Dialog v-model="visible" title="订单列表" width="90%">
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        border
        highlight-current-row
        stripe
        @sort-change="sortChange"
        @filter-change="filterChange"
        show-summary
      >
        <el-table-column type="selection" width="64" />
        <el-table-column type="index" label="序 号" width="80" align="center" />
        <el-table-column sortable="true" label="编 号" align="center" prop="outTradeNo" />
        <el-table-column
          sortable="true"
          label="下单时间"
          align="center"
          prop="createDate"
          :formatter="dateFormatter"
        />
        <el-table-column sortable="true" label="总金额(元)" align="right" prop="totalAmount" />
        <el-table-column label="交易状态" align="center" prop="tradeStateCn" />
        <el-table-column label="类 型" align="center" prop="countItem" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="formData.pageNum"
        v-model:limit="formData.pageSize"
        @pagination="getPage"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" name="detail" setup>
import * as Api from '@/api/userCenter/tradeOrder/tradeOrder'
import { dateFormatter } from '@/utils/formatTime'
const message = useMessage() // 消息弹窗

const visible = ref(false) // 弹窗的是否展示
const loading = ref(false) // 表单的加载中

const total = ref<number>(0) // 列表的总页数
const list = ref([]) // 列表的数据
const formData = reactive({
  shopBuyerId: '',
  pageNum: 1,
  pageSize: 20,
  searchValue: '',
  filters: '',
  sortOrder: 'DESC', // 排序,asc／desc
  sortName: 'create_date' // 排序,字段
})
/** 打开弹窗 */
const open = async (id = '') => {
  visible.value = true
  // 修改时,设置数据
  if (id) {
    loading.value = true
    formData.shopBuyerId = id
    try {
      await getPage()
    } catch (t) {
      console.log(t)
    } finally {
      loading.value = false
    }
  }
}

/** 查询列表 */
const getPage = async () => {
  loading.value = true
  try {
    const data = await Api.getPage(formData)
    list.value = data.list
    total.value = data.total
  } catch (t) {
    loading.value = false
    message.error('查询列表失败')
  } finally {
    loading.value = false
  }
}

const sortChange = async (data) => {
  formData.sortOrder = data.order.replace(/ending$/, '')
  formData.sortName = data.prop

  getPage()
}

defineExpose({ open }) // 提供 open 方法,用于打开弹窗
</script>
<style>
.tag-purple {
  color: #722ed1;
  background: #f9f0ff;
  border-color: #d3adf7;
}

.tag-pink {
  color: #eb2f96;
  background: #fff0f6;
  border-color: #ffadd2;
}
</style>
